package com.example.bitintershipbbs.controller;

import com.example.bitintershipbbs.DTO.CommandList;
import com.example.bitintershipbbs.DTO.Msg;
import com.example.bitintershipbbs.mapper.CommandMapper;
import com.example.bitintershipbbs.mapper.PostMapper;
import com.example.bitintershipbbs.mapper.UserMapper;
import com.example.bitintershipbbs.model.Command;
import com.example.bitintershipbbs.model.Post;
import com.example.bitintershipbbs.model.User;
import javafx.geometry.Pos;
import org.apache.ibatis.annotations.Param;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@CrossOrigin
@Controller
public class CommandController {
    @Autowired
    private CommandMapper commandMapper;

    @Autowired
    private UserMapper userMapper;


    @RequestMapping("/{id}")
    public ResponseEntity<Object> cerateCommand(@PathVariable Integer id,
                                                @Param("description") String description,
                                                HttpServletRequest request){
        Command command = new Command();
        Msg msg = new Msg();

        User user = (User) request.getSession().getAttribute("user");
        if (user != null){
            command.setCreator_id(user.getId());
            command.setPost_id(id);
            command.setDescription(description);
            commandMapper.createCommand(command);
            return ResponseEntity.status(200).body(command);
        }
        else {
            Cookie[] cookies = request.getCookies();
            if (cookies != null) {
                for (Cookie cookie: cookies) {
                    if (cookie.getName().equals("token")) {
                        String token = cookie.getValue();
                        user = userMapper.findByToken(token);
                        request.getSession().setAttribute("user",user);
                        if (user != null) {
                            command.setCreator_id(user.getId());
                            command.setPost_id(id);
                            command.setDescription(description);
                            command.setGmt_create(System.currentTimeMillis());
                            command.setGmt_modified(command.getGmt_create());
                            commandMapper.createCommand(command);
                            return ResponseEntity.status(200).body(command);
                        }
                    }
                }
            }
        }

        msg.setCode(400);
        msg.setMessage("用户未登录");
        return ResponseEntity.status(400).body(msg);
    }

    @GetMapping("/{id}/command")
    public ResponseEntity<Object> getCommandList(@PathVariable Integer id,
                                                 HttpServletRequest request,
                                                 HttpServletResponse response){
        Msg msg = new Msg();
        List<Command> commands = commandMapper.findByPostId(id);
        CommandList commandList = new CommandList();
        commandList.setCommandList(commands);
        commandList.setCount(commands.size());
        return ResponseEntity.status(200).body(commandList);
    }
}
